package com.longge.wechat.controller.web;




import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.longge.wechat.entity.Boss;
import com.longge.wechat.entity.UserMessageEntity;
import com.longge.wechat.service.UserMessageService;


@Controller
public class TestController {
	@Resource
	private UserMessageService userMessageServiceImpl;
	
	@RequestMapping(value = "/user.do", method = RequestMethod.GET)
	public void user(HttpServletRequest request, HttpServletResponse response) {
		List<UserMessageEntity> list = userMessageServiceImpl.findAllUserMessage(Integer.MAX_VALUE, 0);
		for (UserMessageEntity userMessageEntity : list) {
			System.out.println(userMessageEntity.getOpenId()+"---"+userMessageEntity.getContent());
		}
	}
	
	
	@RequestMapping(value = "/test.do", method = RequestMethod.GET)
	public ModelAndView getUserInfo(HttpServletRequest request, HttpServletResponse response) {
		String path = request.getParameter("path");
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("path", path);
		mav.setViewName("test");
		return mav;
	}
	
	//滚动页面动态加载图片
	@RequestMapping(value ="/loadTest.do",method = RequestMethod.POST)
	public void loadTest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		List<UserMessageEntity> list = userMessageServiceImpl.findAllUserMessage(2, 0);
		
		JSONArray json = JSONArray.fromObject(list);
		out.println(json.toString());
		out.flush();
		out.close();
	}

	//线程测试
	@RequestMapping(value ="/thread.do",method = RequestMethod.GET)
	public void thread(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		//final Boss boss = null;
		/*for (int i = 0; i < 100000; i++) {
			boss = Boss.getInstance("大魔王", 100, "boss已死亡，下次打boss活动即将展开，欢迎你的加入", "次boss是女娲娘娘五彩石补天时由神石吸取日月精华而生");
			if(boss.getIsDead() == 0) {
				boss.playBoss(""+i);
			}else {
				System.out.println("恭喜"+boss.getLastUser()+"打死boss");
				break;
			}
			
		}*/
		Boss.getBoss("大魔王", 100, "boss已死亡，下次打boss活动即将展开，欢迎你的加入", "次boss是女娲娘娘五彩石补天时由神石吸取日月精华而生");
		for (int i = 0; i < 100; i++) {
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					for (int j = 0; j < 100; j++) {
						Boss boss = Boss.getInstance();
						if(boss.getIsDead() == 0) {
							boss.fightBoss(""+j);
						}else {
							System.out.println("恭喜"+boss.getLastUser()+"打死boss");
							break;
						}
						
					}
				}
			}).start();
		}
		
	}
}
